//jqGrid的配置信息
if($.jgrid){
    $.jgrid.defaults.width = 1000;
    $.jgrid.defaults.responsive = true;
    $.jgrid.defaults.styleUI = 'Bootstrap';
}

/**
 * Jquery将表单数据转换为对象
 * @returns {{}}
 */
$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
//工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost:8080/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if(r!=null)return  unescape(r[2]); return null;
};
T.p = url;

//全局配置
$.ajaxSetup({
	dataType: "json",
	contentType: "application/json",
	cache: false,
    global: true,
    header:{'Authorization':parent.authValue},
    error: function(jqXHR, textStatus, errorThrown){
    },
    success: function(data){
    }
});
//全局异常处理
$(document).ajaxError(function(evt, req, settings){
    if(req && (req.status === 200||req.status === 0)){ return false; }
    var msg = '';
    if(req && req.responseJSON){
        var json = req.responseJSON;
        msg += json.msg||'系统异常，请重试';
        alertOK(msg,toLogin);
    } else{
        msg = '系统异常，请重试';
        alert(msg);
    }
});
//全局参数设置
$.ajaxPrefilter( function(options, originalOptions, jqXHR){
    if(!options.headers){
        options.headers = {
            'Authorization':parent.authValue
        };
    }else{
        options.headers[parent.authKey] = parent.authValue;
    }
});
function toLogin() {
    window.location="/toLogin"
}

function hasPermission(permission) {
    if (window.parent.permissions.indexOf(permission) > -1) {
        return true;
    } else {
        return false;
    }
}

//重写alert
window.alert = function(msg, callback){
	parent.layer.alert(msg, function(index){
		parent.layer.close(index);
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

window.alertOK = function(msg, callback){
    parent.layer.alert(msg,  {
        closeBtn: 0
    }, function(index){
        parent.layer.close(index);
        if(typeof(callback) === "function"){
            callback("ok");
        }
    });
}

//重写confirm式样框
window.confirm = function(msg, callback){
	parent.layer.confirm(msg, {btn: ['确定','取消']},
	function(){//确定事件
		if(typeof(callback) === "function"){
			callback("ok");
		}
	});
}

//选择一条记录
function getSelectedRow() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    var selectedIDs = grid.getGridParam("selarrrow");
    if(selectedIDs.length > 1){
    	alert("只能选择一条记录");
    	return ;
    }
    
    return selectedIDs[0];
}
/*
**
* 表单加载JSON数据
* @param data
*/
$.fn.formLoadData = function (data) {
    for(key in data) {
        var value = data[key];
        $(this).find("[name='"+key+"'],[name='"+key+"[]']").each(function(){
            var tagName = $(this)[0].tagName;
            var type = $(this).attr('type');
            if(tagName=='INPUT'){
                if(type=='radio'){
                    $(this).attr('checked',$(this).val()==value);
                }else if(type=='checkbox'){
                    var arr = value.split(',');
                    for(var i =0;i<arr.length;i++){
                        if($(this).val()==arr[i]){
                            $(this).attr('checked',true);
                            break;
                        }
                    }
                }else{
                    $(this).val(value);
                }
            }else if(tagName=='SELECT' || tagName=='TEXTAREA'){
                $(this).val(value);
            }
        });
    }
    //手工触发select/input change事件
    $(this).find("input").trigger("change");
    $(this).find("select").trigger("change");
}
//选择多条记录
function getSelectedRows() {
    var grid = $("#jqGrid");
    var rowKey = grid.getGridParam("selrow");
    if(!rowKey){
    	alert("请选择一条记录");
    	return ;
    }
    
    return grid.getGridParam("selarrrow");
}

$.fn.formReset = function () {
    $(this)[0].reset();

    $(this).find("input[type=hidden]").val("");

    //手工触发select/input change事件
    $(this).find("input").trigger("change");
    $(this).find("select").trigger("change");
}